<?php
namespace app\index\controller;
header("Content-Type: text/html;charset=utf-8");
use think\Controller;
class Cart  extends Common
{
    //添加购物车
    public function add()
    {
        $guigeType_id = input('post.guigetype_id');
        $data = db('goods_attributes')->alias('a')->join('goods b','a.goods_id=b.id')->join('goods_guigetype c','a.guigeType_id=c.id')->field('a.id as attributes_id,b.id as goods_id')->where("guigeType_id={$guigeType_id}")->find();
        $data['user_id'] = session('id');
        $data['num'] = 1;
        $ret = db('shopping_car')->insert($data); 
        if($ret==FALSE){
            $this->error("添加购物车失败!",'',1);
        }else{
            $this->success("添加成功,在购物车等亲~",'',2);
        }
    }
    public function addAfter()
    {
        $user_id = session('id');
        $data = db('shopping_car')->where("user_id={$user_id}")->select();
        if($data){
            for($i=0;$i<count($data);$i++){
                $list[$i] = db('goods_attributes')->alias('a')->join('goods b','a.goods_id=b.id')->join('goods_guigetype c','a.guigeType_id=c.id')->field('a.price,a.jifen,b.*,c.name as guigeName,c.pid')->where("a.id={$data[$i]['attributes_id']}")->find();
                if($list[$i]['pid']!=0){
                    $ret = db('goods_guigetype')->where("id={$list[$i]['pid']}")->find();
                    $list[$i]['first_guige_name'] = $ret['name'];
                }else{
                    $list[$i]['first_guige_name'] = '';
                }                
                $list[$i]['price'] = '¥'.$list[$i]['price'];
                $list[$i]['car_id'] = $data[$i]['id'];

                $row=[];
                foreach( $list as $val )
                {
                    $img = json_decode($val['src'],true);    //转换格式,json字符串->php数组
                    if($img!=NULL){
                        $val['src']=array($img[0]);
                    }else{
                        $val['src']=array($val['src']);
                    }
                    $thumb_img = json_decode($val['thumb_src'],true);    //转换格式,json字符串->php数组
                    if($thumb_img!=NULL){
                        $val['thumb_src']=array($img[0]);
                    }else{
                        $val['thumb_src']=array($val['thumb_src']);
                    }
                    $row[] = $val;  //存入新数组$row中
                }

            }
            print_r(json_encode($row));
        }   
    }
    public function delete()
    {
        $car_id = input('post.car_id');
        $ret = db('shopping_car')->where("id={$car_id}")->delete();
        if($ret == false){
            return 0;
        }else{
            echo $car_id;
        }
    }
    public function cart()
    {
        $user_id = session('id');
        $data = db('shopping_car')->where("user_id={$user_id}")->select();
        if($data){
            for($i=0;$i<count($data);$i++){
                $list[$i] = db('goods_attributes')->alias('a')->join('goods b','a.goods_id=b.id')->join('goods_guigetype c','a.guigeType_id=c.id')->join('shopping_car d','a.id=d.attributes_id')->field('a.price,a.jifen,b.*,c.name as guigeName,c.pid,d.num')->where("a.id={$data[$i]['attributes_id']}")->find();
                if($list[$i]['pid']!=0){
                    $ret = db('goods_guigetype')->where("id={$list[$i]['pid']}")->find();
                    $list[$i]['first_guige_name'] = $ret['name'];
                }else{
                    $list[$i]['first_guige_name'] = '';
                }                
                $list[$i]['car_id'] = $data[$i]['id'];
                $row=[];
                foreach( $list as $val )
                {
                    $img = json_decode($val['src'],true);    //转换格式,json字符串->php数组
                    if($img!=NULL){
                        $val['src']=array($img[0]);
                    }else{
                        $val['src']=array($val['src']);
                    }
                    $thumb_img = json_decode($val['thumb_src'],true);    //转换格式,json字符串->php数组
                    if($thumb_img!=NULL){
                        $val['thumb_src']=array($img[0]);
                    }else{
                        $val['thumb_src']=array($val['thumb_src']);
                    }
                    $row[] = $val;  //存入新数组$row中
                }
            }
            $totalPrice = '';
            foreach ($row as $k => $v) {
                $totalPrice += $v['price'];
            }            
        }else{
            $totalPrice = null;
            $row = null;
        }
        $this->assign('totalPrice',$totalPrice);
        $this->assign('row',$row);
        return view();
    }

    public function wishlist()
    {
        return view();
    }

    public function checkout()
    {   
        $guigetype_id = input('get.guigetype_id');
        $this->assign('guigetype_id',$guigetype_id);
        $id = session('id');
        $user_list = db('user')->where("id={$id}")->find();
        if(isset($_POST['address_id'])){
            $address_id = $_POST['address_id'];
            $address_list = db('user')->alias('a')->join('user_address b','a.id=b.user_id')->field('b.*,a.address_id')->where("a.id={$id} and b.id={$address_id}")->find();
        }else{
            $address_id = $user_list['address_id'];
            $address_list = db('user')->alias('a')->join('user_address b','a.id=b.user_id')->field('b.*,a.address_id')->where("a.id={$id} and b.id={$address_id}")->find();
        }
        $guigeType_id = input('get.guigetype_id');
        $list = db('goods_attributes')->alias('a')->join('goods b','a.goods_id=b.id')->join('goods_guigetype c','a.guigeType_id=c.id')->field('a.price,a.jifen,b.*,c.name as guigeName,c.pid')->where("guigeType_id={$guigeType_id}")->find();
        $ret = db('goods_guigetype')->where("id={$list['pid']}")->find();
        $list['first_guige_name'] = $ret['name'];

        $img = json_decode($list['src'],true);    //转换格式,json字符串->php数组
        if($img!=NULL){
            $list['src']=array($img[0]);
        }else{
            $list['src']=array($list['src']);
        }
        $thumb_img = json_decode($list['thumb_src'],true);    //转换格式,json字符串->php数组
        if($thumb_img!=NULL){
            $list['thumb_src']=array($img[0]);
        }else{
            $list['thumb_src']=array($list['thumb_src']);
        }
        //使用了优惠券后
        if(isset($_GET['coupon_id'])){
            $coupon_id = input('get.coupon_id');
            $coupon_list = db('coupon')->where("id={$coupon_id}")->find();
            $this->assign('coupon_list',$coupon_list);
        }
        $this->assign('guigeType_id',$guigeType_id);
        $this->assign('address_list',$address_list);
        $this->assign('list',$list);
        return view();
    }
    public function checkoutCar()
    {
        if(isset($_POST['zongji'])){
            $post_data = input('post.');
            session('post_data',$post_data);
        }        
        $post_list = session('post_data');
        //获取收货地址数据
        $user_id = session('id');
        $user_list = db('user')->where("id={$user_id}")->find();
        if(isset($_POST['address_id'])){
            $address_id = $_POST['address_id'];
            $address_list = db('user')->alias('a')->join('user_address b','a.id=b.user_id')->field('b.*,a.address_id')->where("a.id={$user_id} and b.id={$address_id}")->find();
        }else{
            $address_id = $user_list['address_id'];
            $address_list = db('user')->alias('a')->join('user_address b','a.id=b.user_id')->field('b.*,a.address_id')->where("a.id={$user_id} and b.id={$address_id}")->find();
        }
        // dump($address_list);die;
        $this->assign('address_list',$address_list);

        //获取购物车列表数据
        $data = db('shopping_car')->where("user_id={$user_id}")->select();
        for($i=0;$i<count($data);$i++){
            $list[$i] = db('goods_attributes')->alias('a')->join('goods b','a.goods_id=b.id')->join('goods_guigetype c','a.guigeType_id=c.id')->join('shopping_car d','a.id=d.attributes_id')->field('a.price,a.jifen,a.id as attributes_id,b.*,c.name as guigeName,c.pid,d.num')->where("a.id={$data[$i]['attributes_id']}")->find();
            if($list[$i]['pid']!=0){
                $ret = db('goods_guigetype')->where("id={$list[$i]['pid']}")->find();
                $list[$i]['first_guige_name'] = $ret['name'];
            }else{
                $list[$i]['first_guige_name'] = '';
            }                
            $list[$i]['car_id'] = $data[$i]['id'];
            $row=[];
            foreach( $list as $val )
            {
                $img = json_decode($val['src'],true);    //转换格式,json字符串->php数组
                if($img!=NULL){
                    $val['src']=array($img[0]);
                }else{
                    $val['src']=array($val['src']);
                }
                $thumb_img = json_decode($val['thumb_src'],true);    //转换格式,json字符串->php数组
                if($thumb_img!=NULL){
                    $val['thumb_src']=array($img[0]);
                }else{
                    $val['thumb_src']=array($val['thumb_src']);
                }
                $row[] = $val;  //存入新数组$row中
            }
        }
        $totalPrice = '';
        foreach ($row as $k => $v) {
            $totalPrice += $v['price'];
        }  
        foreach ($row as $k => $v) {
            for($i=0;$i<count($post_list['num']);$i++){
                if($k==$i){
                    $row[$k]['num']=$post_list['num'][$i];
                }                
            }
            for($i=0;$i<count($post_list['xiaoji']);$i++){
                if($k==$i){
                    $row[$k]['xiaoji']=$post_list['xiaoji'][$i];
                }                
            }            
        }
        //使用了优惠券后
        if(isset($_GET['coupon_id'])){
            $coupon_id = input('get.coupon_id');
            $coupon_list = db('coupon')->where("id={$coupon_id}")->find();
            $this->assign('coupon_list',$coupon_list);
        }
        $this->assign('zongji',$post_list['zongji']);
        $this->assign('totalPrice',$totalPrice);
        $this->assign('list',$row);
        return view('checkout-car');
    }
    public function myAddress()
    {
        $guigeType_id = input('guigetype_id');
        $user_id = session('id');
        $list = db('user_address')->alias('a')->join('user b','a.user_id=b.id')->field('a.*,b.address_id')->where("user_id=$user_id")->select();
        $this->assign('list',$list);
        $this->assign('guigetype_id',$guigeType_id);
        return view('myaddress');
    }

    //地址添加页面
    public function addAd()
    {
        $guigeType_id = input('get.guigetype_id');
        $this->assign('guigetype_id',$guigeType_id);
        return view('add');
    }

    //添加新地址
    public function insert()
    {
        $user_id = session('id');
        $shouhuo_name = input('post.shouhuo_name');
        $shouhuo_tell = input('post.shouhuo_tell');
        $shouhuo_address = input('post.shouhuo_address');

        $data=[
            'user_id' => $user_id,
            'shouhuo_address' => $shouhuo_address,
            'shouhuo_tell' => $shouhuo_tell,
            'shouhuo_name' => $shouhuo_name,
        ];

        $ret = db('user_address')->insert($data);
        $guigeType_id = input('guigetype_id');
        // dump($guigeType_id);die;
        if($ret==false){
            $this->error('添加失败');
        }else{
            if($guigeType_id!=null){
                $this->success('添加成功',url('index/cart/myAddress',['guigetype_id'=>$guigeType_id]));
            }
            $this->success('添加成功','index/cart/myAddress');
        }
    }

    //地址修改页面
    public function edit()
    {
        $guigetype_id = input('get.guigetype_id');
        $id = input('get.id');      
        // dump($id);die;
        $list = db('user_address')->alias('a')->join('user b','a.user_id=b.id')->field('a.*,b.address_id')->where("a.id=$id")->find();
        // dump($list);die;
        $this->assign('list',$list);
        $this->assign('guigetype_id',$guigetype_id);
        return view();
    }

    //修改地址
    public function update()
    {
        $guigeType_id = input('get.guigetype_id');
        $id = input('get.id');
        $data = input('post.');
        $ret = db('user_address')->where("id='{$id}'")->update($data);
        if($ret==false){
            $this->error('修改失败');
        }else{
            if($guigeType_id!=null){
                $this->success('添加成功',url('index/cart/myAddress',['guigetype_id'=>$guigeType_id]));
            }
            $this->success('添加成功','index/cart/myAddress');
        }
    }

    //删除地址
    public function deleteAdd()
    {
        $guigeType_id = input('get.guigetype_id');
        $id = input('get.id');
        // dump($id);die;
        $ret = db('user_address')->where("id={$id}")->delete();
        if($ret==false){
            $this->error('删除失败');
        }else{
            if($guigeType_id!=null){
                $this->success('添加成功',url('index/cart/myAddress',['guigetype_id'=>$guigeType_id]));
            }
            $this->success('添加成功','index/cart/myAddress');
        }
    }
}